package com.party.controller.user.learning;

import com.party.common.Tools;
import com.party.model.*;
import com.party.service.exam.ExamService;
import com.party.service.study.CurriculumService;
import com.party.service.study.LearningContentService;
import com.party.service.study.TestService;
import com.party.service.study.User2ContentService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 * User: beyond
 * Date: 14-10-8
 * Time: 下午10:18
 * To change this template use File | Settings | File Templates.
 */
@Controller
@RequestMapping(value = "/test/")
public class LearningTestController {


    @Resource
    CurriculumService curriculumService;

    @Resource
    TestService testService;
    @Resource
    ExamService examService;
    private int pageSize = 18;

    /**
     * 首页
     * @return
     */
    @RequestMapping(value = "testList")
    public ModelAndView index(HttpSession session) {
        ModelAndView modelAndView = new ModelAndView();
        List<Curriculum> currList = new ArrayList<Curriculum>();//学习完成后的考试
        List<Exam> examList = new ArrayList<Exam>();//一般的考试
        if ( session.getAttribute("sysUser") != null) {
            int uid = ((SysUser) session.getAttribute("sysUser")).getUid();
            currList = curriculumService.getByUidNew(uid);

            List<Exam> tempList = this.examService.getAllExam();
            if(tempList != null && tempList.size()>0)
            {
                List<Exam> myList = this.examService.getByUid(uid);
                if(myList != null && myList.size()>0)
                {
                    boolean isStay = true;
                    //剔除那些已经考试结束的
                    for(Exam e1:tempList)
                    {
                        for(Exam e2:myList)
                        {
                            if(e2.getId() == e1.getId())
                            {
                                isStay = false;
                            }
                        }
                        if(isStay)
                        {
                            examList.add(e1);
                        }
                    }
                }
                else
                {
                    examList= tempList;
                }
            }

        }
        else //未登陆。跳转到登陆页面
        {
            modelAndView.setViewName("/login");
            return modelAndView;
        }
        modelAndView.addObject("currList",currList);
        modelAndView.addObject("examList",examList);
        modelAndView.setViewName("user/learning/test");
        return modelAndView;
    }

    /**
     * 考试结果查询
     * @param session
     * @return
     */
    @RequestMapping(value = "result")
    public ModelAndView result(HttpSession session) {
        ModelAndView modelAndView = new ModelAndView();
        List<Curriculum> currList = new ArrayList<Curriculum>();//学习完成后的考试
        List<Exam> examList = new ArrayList<Exam>();//一般的考试
        if (session.getAttribute("sysUser") != null) {
            int uid = ((SysUser) session.getAttribute("sysUser")).getUid();
            currList = curriculumService.getByUidOld(uid);
            examList = this.examService.getByUid(uid);
        }
        else //未登陆。跳转到登陆页面
        {
            modelAndView.setViewName("/login");
            return modelAndView;
        }
        modelAndView.addObject("currList",currList);
        modelAndView.addObject("examList",examList);
        modelAndView.setViewName("user/learning/result");
        return modelAndView;
    }

}
